import { IBodyCreateConfig } from "../core/rpg/types";
import sdk from '../core/index';


/**
 * 随从服务类
 * 职责：
 * 1. 随从管理
 * 2. 随从创建和删除
 * 3. 随从战斗支持
 */
export class FollowService {

    /**
     * 添加随从
     * @param currentFollows 当前随从列表
     * @param followData 随从数据
     * @returns 添加结果
     */
    static addFollow(currentFollows: IBodyCreateConfig[], followData: IBodyCreateConfig): {
        success: boolean;
        message: string;
        newFollows: IBodyCreateConfig[];
    } {
        // 检查随从数量限制
        if (currentFollows.length >= 5) {
            return {
                success: false,
                message: '随从数量已达上限',
                newFollows: currentFollows
            };
        }

        const newFollows = [...currentFollows, followData];
        
        return {
            success: true,
            message: `获得随从: ${followData.name}`,
            newFollows
        };
    }

    /**
     * 删除随从
     * @param currentFollows 当前随从列表
     * @param index 随从索引
     * @returns 删除结果
     */
    static removeFollow(currentFollows: IBodyCreateConfig[], index: number): {
        success: boolean;
        message: string;
        newFollows: IBodyCreateConfig[];
        removedFollow?: IBodyCreateConfig;
    } {
        if (index < 0 || index >= currentFollows.length) {
            return {
                success: false,
                message: '随从不存在',
                newFollows: currentFollows
            };
        }

        const newFollows = [...currentFollows];
        const removedFollow = newFollows.splice(index, 1)[0];
        
        return {
            success: true,
            message: `删除随从: ${removedFollow.name}`,
            newFollows,
            removedFollow
        };
    }

    /**
     * 获取随从状态
     * @param follows 随从列表
     * @returns 随从状态
     */
    static getFollowStatus(follows: IBodyCreateConfig[]): {
        followCount: number;
        totalLevel: number;
        averageLevel: number;
        totalSkills: number;
    } {
        const totalLevel = follows.reduce((sum, follow) => sum + (follow.level || 1), 0);
        const totalSkills = follows.reduce((sum, follow) => sum + (follow.skill?.length || 0), 0);
        
        return {
            followCount: follows.length,
            totalLevel,
            averageLevel: follows.length > 0 ? totalLevel / follows.length : 0,
            totalSkills
        };
    }

    /**
     * 创建随从列表卡片
     * @param follows 随从列表
     * @returns 随从卡片
     */
    static createFollowListCard(follows: IBodyCreateConfig[]): any {
        const card = sdk.tool.card.create();
        card.title('我的随从');
        
        if (follows.length === 0) {
            card.l('暂无随从');
        } else {
            follows.forEach((follow, index) => {
                card.l(`[${index + 1}]${follow.emoji} ${follow.name} (Lv.${follow.level || 1})`);
            });
        }
        
        return card;
    }

    /**
     * 给所有宠物添加经验
     * @param pets 宠物列表
     * @param amount 经验值
     * @returns 经验添加结果
     */
    static addExperienceToAllPets(pets: any[], amount: number): {
        success: boolean;
        message: string;
        updatedPets: {
            pet: any;
            leveledUp: boolean;
            oldLevel: number;
            newLevel: number;
        }[];
    } {
        if (pets.length === 0) {
            return {
                success: false,
                message: '没有宠物可以获得经验',
                updatedPets: []
            };
        }

        const expPerPet = amount / pets.length;
        const updatedPets: {
            pet: any;
            leveledUp: boolean;
            oldLevel: number;
            newLevel: number;
        }[] = [];

        pets.forEach(pet => {
            const rpg = pet.getComponent('rpg'); // 获取rpg组件
            const oldLevel = rpg.level;
            
            rpg.addExp(expPerPet);
            
            const newLevel = rpg.level;
            const leveledUp = newLevel > oldLevel;
            
            updatedPets.push({
                pet,
                leveledUp,
                oldLevel,
                newLevel
            });
        });

        return {
            success: true,
            message: `所有宠物获得了经验: ${expPerPet.toFixed(1)}`,
            updatedPets
        };
    }
} 